# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response,redirect
import logging

from musiclib.models import Music
from myuser.usercheck import getUser

def index(request):
    """
    show the music lib management
    """
    dic = {}
    user = getUser(request)
    if user == None:
        return redirect("/basicuser/login")
    else:
        dic['user'] = user
        dic['username'] = user.nickname

        #get recent add music
        mlist = Music.all().order('-addtime').fetch(limit=10)
        logging.info("the length of mlist is : "+str(len(mlist)))
        tmplist = []
        for mitem in mlist:
            tmpdic = {}
            tmpdic['key'] = mitem.key()
            tmpdic['name'] = mitem.mname
            tmplist.append(tmpdic)
        dic['mlist'] = tmplist

        return render_to_response("musiclib/index.html",dic)

def musicpage(request):
    """
    may be more action for admin user
    """
    return HttpResponse("hola")

def artistpage(request):
    return HttpResponse("hola")

def albumpage(request):
    return HttpResponse("hola")

def addmusicpage(request):
    dic = {}
    user = getUser(request)
    if user == None and  :
        if user.nickname != "shrekwang":
            return HttpResponse("not shrekwang, no authorize to add music")
        else:
            return redirect("/basicuser/login")
    else:
        # simple show the page 
        # all the other is relized with ajax
        return render_to_response("/musiclib/addmusic.html",dic)

def recentaddmusic(request):
    """
    show recent add music
    """
    mlist = Music.all().order("-addtime").fetch(10)
    
